import 'package:flutter/material.dart';

class ProfilePage extends StatelessWidget {
  final double height;
  final double weight;
  final String gender;
  final DateTime? birthDate;
  final VoidCallback onSettingsNavigate;

  const ProfilePage({
    super.key,
    required this.height,
    required this.weight,
    required this.gender,
    required this.birthDate,
    required this.onSettingsNavigate,
  });

  int get age {
    if (birthDate == null) return 0;
    final now = DateTime.now();
    int age = now.year - birthDate!.year;
    if (now.month < birthDate!.month ||
        (now.month == birthDate!.month && now.day < birthDate!.day)) {
      age--;
    }
    return age;
  }

  double get bmr {
    if (gender == '男性') {
      return 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age);
    } else {
      return 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);
    }
  }

  double get bmi => weight / ((height / 100) * (height / 100));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人数据'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Card(
              elevation: 4,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    _buildInfoRow(context, '身高', '${height.toStringAsFixed(1)} cm'),
                    _buildInfoRow(context, '体重', '${weight.toStringAsFixed(1)} kg'),
                    _buildInfoRow(context, '性别', gender),
                    _buildInfoRow(context, '年龄', '$age 岁'),
                    const Divider(height: 24),
                    _buildInfoRow(context, 'BMI', bmi.toStringAsFixed(1)),
                    _buildInfoRow(context, '基础代谢率', '${bmr.toStringAsFixed(1)} kcal/day'),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 24),
            ElevatedButton.icon(
              icon: const Icon(Icons.edit),
              label: const Text('编辑个人信息'),
              onPressed: onSettingsNavigate,
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 12),
                textStyle: const TextStyle(fontSize: 18),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(BuildContext context, String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label, style: Theme.of(context).textTheme.titleMedium),
          Text(value, style: Theme.of(context).textTheme.titleLarge),
        ],
      ),
    );
  }
}
